package com.dingli.servlet;

import com.dingli.dao.UserDAO;
import com.dingli.dao.UserLevelDAO;
import com.dingli.entity.User;
import com.dingli.entity.UserLevel;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/UserLevelServlet.do")
public class UserLevelServlet extends HttpServlet {

    private UserDAO userDAO = new UserDAO();
    private UserLevelDAO userLevelDAO = new UserLevelDAO();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取操作指令
        String action = req.getParameter("action");
        if (action.equals("findUserLevelByUserId")){
            String uid = req.getParameter("uid");
            UserLevel userLevel = new UserLevel();
            User user = userDAO.selectById(Integer.parseInt(uid));
            userLevel.setUser(user);
            UserLevel userLevel1 = userLevelDAO.selectById(Integer.parseInt(uid));
            if (userLevel1 != null){
                userLevel.setId(userLevel1.getId());
                userLevel.setUid(userLevel1.getUid());
                userLevel.setLevel(userLevel1.getLevel());
                userLevel.setLimit(userLevel1.getLimit());
            }
            req.setAttribute("userLevel",userLevel);
            req.getRequestDispatcher("./jsp/edtUserLevel.jsp").forward(req, resp);
        }else if (action.equals("addOrEdt")){
            String uid = req.getParameter("uid");
            String level = req.getParameter("level");
            System.out.println(level);
            if (level != null && level != ""){
                UserLevel userLevel = userLevelDAO.selectById(Integer.parseInt(uid));
                if (userLevel == null){
                    UserLevel userLevel1 = new UserLevel();
                    userLevel1.setLevel(Integer.parseInt(level));
                    userLevel1.setUid(Integer.parseInt(uid));
                    if (Integer.parseInt(level) == 0){
                        userLevel1.setLimit(50000.00);
                    }else if (Integer.parseInt(level) == 1){
                        userLevel1.setLimit(100000.00);
                    }else if (Integer.parseInt(level) ==2){
                        userLevel1.setLimit(150000.00);
                    }
                    int count = userLevelDAO.insert(userLevel1);
                    if (count > 0){
                        req.setAttribute("message", "添加成功！");
                        req.getRequestDispatcher("./UserServlet.do?action=show").forward(req, resp);
                    }else {
                        req.setAttribute("message", "添加失败！");
                        req.getRequestDispatcher("./UserServlet.do?action=show").forward(req, resp);
                    }
                }else {
                    UserLevel userLevel1 = new UserLevel();
                    double money = 0;
                    if (userLevel.getLevel() == 0){
                        money = 50000.00;
                    }else if(userLevel.getLevel() == 1){
                        money = 100000.00;
                    }else if (userLevel.getLevel() == 2){
                        money = 150000.00;
                    }

                    money -= userLevel.getLimit();

                    userLevel1.setLevel(Integer.parseInt(level));
                    userLevel1.setUid(Integer.parseInt(uid));
                    if (Integer.parseInt(level) == 0){
                        userLevel1.setLimit(50000.00-money);
                    }else if (Integer.parseInt(level) == 1){
                        userLevel1.setLimit(100000.00-money);
                    }else if (Integer.parseInt(level) ==2){
                        userLevel1.setLimit(150000.00-money);
                    }
                    int count = userLevelDAO.updateById(userLevel1);
                    if (count > 0){
                        req.setAttribute("message", "修改成功！");
                        req.getRequestDispatcher("./UserServlet.do?action=show").forward(req, resp);
                    }else {
                        req.setAttribute("message", "修改失败！");
                        req.getRequestDispatcher("./UserServlet.do?action=show").forward(req, resp);
                    }
                }
            }else {
                req.setAttribute("message", "请选择星级！");
                req.getRequestDispatcher("./UserServlet.do?action=show").forward(req, resp);
            }

        }
    }
}
